package com.ylr.blog.framework.controller.web;

import java.util.List;
import java.util.Objects;

import com.ylr.base.common.annotation.Resubmit;
import com.ylr.base.common.dto.PageDto;
import com.ylr.blog.common.dto.blog_tag.BlogTagDto;
import com.ylr.base.common.jsr303.group.Save;
import com.ylr.base.common.jsr303.group.Update;
import com.ylr.base.common.tool.Result;
import com.ylr.blog.common.vo.blog_tag.BlogTagFormVo;
import com.ylr.blog.common.vo.blog_tag.BlogTagVo;
import com.ylr.blog.framework.service.BlogTagService;
import lombok.AllArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wyw
 * date: 2023-05-23 22:24:25
 * className: BlogTagWebController
 * version: 1.0
 * description:
 */
@Validated
@RestController
@RequestMapping("/web/blog-tag")
@AllArgsConstructor
public class BlogTagWebController {

    private static final Logger log = LoggerFactory.getLogger(BlogTagWebController.class);

    private final BlogTagService blogTagService;

    /**
     * 保存博客标签接口
     * @param blogTagVo 参数对象
     */
    @Resubmit(spEL = "'saveBlogTag'.concat(#userPo.id)", message = "保存博客标签")
    @PostMapping("/save")
    public Result<Object> saveBlogTag(@Validated(Save.class) @RequestBody BlogTagVo blogTagVo) {
        log.info("web => 开始保存博客标签操作。{}", blogTagVo);

        blogTagService.saveBlogTag(blogTagVo);

        log.info("web => 保存博客标签操作结束");
        return Result.success();
    }

    /**
     * 分页查询博客标签接口
     * @param blogTagFormVo 参数对象
     * @return 博客标签分页数据
     */
    @PostMapping("/page/list")
    public Result<PageDto<BlogTagDto>> pageBlogTagList(@RequestBody BlogTagFormVo blogTagFormVo) {
        log.info("web => 开始分页查询博客标签操作。{}", blogTagFormVo);

        PageDto<BlogTagDto> pageDto = blogTagService.pageBlogTagList(blogTagFormVo);

        log.info("web => 分页查询博客标签操作结束");
        return Result.success(pageDto);
    }

    /**
     * 获取博客标签接口
     * @param id 主键
     * @return 博客标签对象
     */
    @GetMapping("/get")
    public Result<BlogTagDto> getBlogTag(@RequestParam("id") Long id) {
        log.info("web => 开始获取博客标签操作。id={}", id);

        BlogTagDto blogTagDto = blogTagService.getBlogTag(id);

        log.info("web => 获取博客标签操作结束");
        return Result.success(blogTagDto);
    }

    /**
     * 获取下一个排序等级接口
     * @return 排序等级值
     */
    @GetMapping("/next/sort")
    public Result<Integer> getNextSort() {
        log.info("web => 开始获取下一个排序等级操作");

        Integer sort = blogTagService.getNextSort();

        log.info("web => 获取下一个排序等级操作结束");
        return Result.success(sort);
    }

    /**
     * 修改博客标签接口
     * @param blogTagVo 参数对象
     */
    @Resubmit(spEL = "'updateBlogTag'.concat(#userPo.id)", message = "保存博客标签")
    @PostMapping("/update")
    public Result<Object> updateBlogTag(@Validated(Update.class) @RequestBody BlogTagVo blogTagVo) {
        log.info("web => 开始修改博客标签操作。{}", blogTagVo);

        blogTagService.updateBlogTag(blogTagVo);

        log.info("web => 修改博客标签操作结束");
        return Result.success();
    }

    /**
     * 删除博客标签接口
     * @param idList 主键列表
     */
    @Resubmit(spEL = "'deleteBlogTag'.concat(#userPo.id)", message = "")
    @DeleteMapping("/delete")
    public Result<Object> deleteBlogTag(@RequestBody List<Long> idList) {
        log.info("web => 开始删除博客标签。idList={}", idList);

        for (int i = 0; i < idList.size(); i++) {
            if (Objects.isNull(idList.get(i))) {
                return Result.requestFail("参数列表，第" + (i + 1) + "个主键值错误");
            }
        }

        blogTagService.deleteBlogTag(idList);

        log.info("web => 删除博客标签结束");
        return Result.success();
    }

}
